package io.shuttle.mbe.api

import io.shuttle.mbe.api.annotation.ChromeMinVersion
import io.shuttle.mbe.api.annotation.ForegroudOnly
import io.shuttle.mbe.api.annotation.PromiseStyleMinVersion
import io.shuttle.mbe.api.types.MediaStream
import io.shuttle.mbe.api.types.Value1Function
import io.shuttle.mbe.core.Promise
////////////////////
// TabCapture
////////////////////
/**
 * Use the `Browser.tabCapture` API to interact with tab media streams.
 *
 * Permissions: "tabCapture"
 */
interface TabCapture {
    // Captures the visible area of the currently active tab. Capture can only be started on the currently active tab after the extension has been invoked, similar to the way that activeTab works. Capture is maintained across page navigations within the tab, and stops when the tab is closed, or the media stream is closed by the extension.
    @ForegroudOnly
     fun capture(
        // Configures the returned media stream.
        options: CaptureOptions,
        callback: Value1Function<MediaStream>
    )

    // Returns a list of tabs that have requested capture or are being captured, i.e. status != stopped and status != error. This allows extensions to inform the user that there is an existing tab capture that would prevent a new tab capture from succeeding (or to prevent redundant requests for the same tab).
    @PromiseStyleMinVersion(116)
     fun getCapturedTabs(callback: Value1Function<List<CaptureInfo>>? = null): Promise<List<CaptureInfo>>

    // Creates a stream ID to capture the target tab. Similar to chrome.tabCapture.capture() method, but returns a media stream ID, instead of a media stream, to the consumer tab.
    // @callback streamId
    @ChromeMinVersion(71)
    @PromiseStyleMinVersion(116)
     fun getMediaStreamId(
        options: GetMediaStreamOptions?,
        callback: Value1Function<String>? = null
    )

    // Event fired when the capture status of a tab changes. This allows extension authors to keep track of the capture status of tabs to keep UI elements like page actions in sync.
     val onStatusChanged: Events.Event<Value1Function<CaptureInfo>>

    data class CaptureInfo(
        // Whether an element in the tab being captured is in fullscreen mode.
        var fullscreen: Boolean,
        // The new capture status of the tab.
        var status: TabCaptureState,
        // The id of the tab whose status changed.
        var tabId: Number,
    )

    data class CaptureOptions(
        var audio: Boolean?,
        var audioConstraints: MediaStreamConstraints?,
        var video: Boolean,
        var videoConstraints: MediaStreamConstraints?
    )

    @ChromeMinVersion(71)
    data class GetMediaStreamOptions(
        // Optional tab id of the tab which will later invoke getUserMedia() to consume the stream. If not specified then the resulting stream can be used only by the calling extension. The stream can only be used by frames in the given tab whose security origin matches the consumber tab's origin. The tab's origin must be a secure origin, e.g. HTTPS.
        var consumerTabId: Number?,
        // Optional tab id of the tab which will be captured. If not specified then the current active tab will be selected. Only tabs for which the extension has been granted the activeTab permission can be used as the target tab.
        var targetTabId: Number?,
    )

    data class MediaStreamConstraints(
        var mandatory: Any,
        var optional: Any?,
    )

    enum class TabCaptureState {
        pending,
        active,
        stopped,
        error
    }
}